//
//  TransferGroup.m
//  iMadeCoreData
//
//  Created by Edward Knutson on 10/13/09.
//  Copyright 2009 Six Four Systems. All rights reserved.
//

#import "TransferGroup.h"

#import <CoreLocation/CoreLocation.h>

#import "Convert.h"
#import "Group.h"
#import "Location.h"
#import "Tag.h"

@implementation TransferGroup

@synthesize name, logoUrl, groupDescription, location, icsUrl, rssUrl, servicesUrl;
@synthesize tags, lastUpdated, inactive, subscribed;

- (id)init {
	if(self = [super init]) {
		inactive = NO;
		subscribed = NO;
	}
	return self;
}

- (id)initWithName:(NSString *)groupName withIcsUrl:(NSString *)groupIcsUrl {
	if(groupName == nil || groupIcsUrl == nil)
		return nil;
	if(self = [self init]) {
		self.name = groupName;
		self.icsUrl = groupIcsUrl;
	}
	return self;
}

- (id)initWithGroup:(Group *)group {
	if(self = [self initWithName:group.name withIcsUrl:group.icsUrl]) {
		self.logoUrl = group.logoUrl;
		self.groupDescription = group.groupDescription;
		if(group.tags != nil) {
			NSArray *allTags = [group.tags allObjects];
			NSMutableArray *tempTags = [[NSMutableArray alloc] init];
			self.tags = tempTags;
			[tempTags release];
			for(int i=0; i<[allTags count]; i++) {
				[tempTags addObject:[(Tag *)[allTags objectAtIndex:i] name]];
			}
		}
		if(group.location != nil) {
			CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:[group.location.latitude doubleValue] longitude:[group.location.longitude doubleValue]];
			self.location = tempLocation;
			[tempLocation release];
		}
		self.rssUrl = group.rssUrl;
		self.servicesUrl = group.servicesUrl;
		self.lastUpdated = group.lastUpdated;
		self.inactive = [group.inactive boolValue];
		self.subscribed = [group.subscribed boolValue];
	}
	return self;
}

- (id)initWithDictionary:(NSDictionary *)dict {
	NSLog(@"dict=%@", dict);
	if(self = [self initWithName:[dict valueForKey:@"Name"] withIcsUrl:[dict valueForKey:@"IcsUrl"]]) {
		self.logoUrl = [dict valueForKey:@"LogoUrl"];
		self.groupDescription = [dict valueForKey:@"Description"];
		NSString *latitudeStr = [dict valueForKey:@"LocationLatitude"];
		NSString *longitudeStr = [dict valueForKey:@"LocationLongitude"];
		if(latitudeStr != nil && longitudeStr != nil) {
//			NSLog(@"lat=%@, long=%@", latitudeStr, longitudeStr);
			NSNumber *latitude = [Convert floatFromString:latitudeStr];
			NSNumber *longitude = [Convert floatFromString:longitudeStr];
			if(latitude != nil && longitude != nil)
				self.location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
//			NSLog(@"location=%@", self.location);
		}
		self.rssUrl = [dict valueForKey:@"RssUrl"];
		self.servicesUrl = [dict valueForKey:@"ServicesUrl"];
	}
	self.tags = [dict valueForKey:@"Tags"];
	NSLog(@"\ntags=%@\n", self.tags);
	
	self.lastUpdated = [Convert dateFromString:[dict valueForKey:@"LastUpdated"]];
	NSString *inactiveStr = [dict valueForKey:@"Inactive"];
	if(inactiveStr != nil) {
		self.inactive = [[Convert longLongFromString:inactiveStr] boolValue];
	}
	NSString *subscribedStr = [dict valueForKey:@"Subscribed"];
	if(subscribedStr != nil) {
		self.subscribed = [[Convert longLongFromString:subscribedStr] boolValue];
	}
	return self;
}

- (void)dealloc {
	[name release];
	[logoUrl release];
	[groupDescription release];
	[location release];
	[icsUrl release];
	[rssUrl release];
	[servicesUrl release];
	[tags release];
	[lastUpdated release];
	[super dealloc];
}

@end
